﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CaiBaby.Common;
using CaiBaby.Entity;

namespace CaiBaby.DAL
{
    /// <summary>
    /// 存储数据访问层
    /// </summary>
    public class SaveObjiect
    {
        /// <summary>
        /// 新增存储介质
        /// </summary>
        /// <param name="saveObjiectInfo">存储实体</param>
        /// <returns>返回存储介质ID</returns>
        public int Add(SaveObjiectInfo saveObjiectInfo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO ac_SaveObjiect(");
            strSql.Append("UId,SaveName,Moneys)");
            strSql.Append(" VALUES (");
            strSql.Append("@UId,@SaveName,@Moneys)");
            strSql.Append(";SELECT @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@UId", SqlDbType.Int,4),
					new SqlParameter("@SaveName", SqlDbType.VarChar,32),
					new SqlParameter("@Moneys", SqlDbType.Money,8)};
            parameters[0].Value = saveObjiectInfo.UId;
            parameters[1].Value = saveObjiectInfo.SaveName;
            parameters[2].Value = saveObjiectInfo.Moneys;

            object obj = SQLHelper.GetSingle(ConfigHelper.CaiBabyDBConnStr,CommandType.Text,strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新存储介质
        /// </summary>
        /// <param name="saveObjiectInfo">存储实体</param>
        /// <returns>返回是否成功</returns>
        public bool Update(SaveObjiectInfo saveObjiectInfo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE ac_SaveObjiect SET ");
            strSql.Append("UId=@UId,SaveName=@SaveName,Moneys=@Moneys ");
            strSql.Append("WHERE Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@UId", SqlDbType.Int,4),
					new SqlParameter("@SaveName", SqlDbType.VarChar,32),
					new SqlParameter("@Moneys", SqlDbType.Money,8),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = saveObjiectInfo.UId;
            parameters[1].Value = saveObjiectInfo.SaveName;
            parameters[2].Value = saveObjiectInfo.Moneys;
            parameters[3].Value = saveObjiectInfo.Id;

            int rows = SQLHelper.ExecuteSql(ConfigHelper.CaiBabyDBConnStr,CommandType.Text,strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除存储介质
        /// </summary>
        /// <param name="id">存储介质ID</param>
        /// <returns>返回是否成功</returns>
        public bool Delete(int id)
        {
            string strSql = "DELETE FROM ac_SaveObjiect WHERE Id=@Id";
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = id;
            int rows = SQLHelper.ExecuteSql(ConfigHelper.CaiBabyDBConnStr, CommandType.Text, strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
